package com.zzl.web;

import com.alibaba.fastjson.JSONObject;
import com.zzl.entity.Student;
import com.zzl.services.StudentServices;
import com.zzl.services.impl.StudentServicrImpl;
import com.zzl.util.DateUtil;
import com.zzl.util.DbUtil;
import com.zzl.util.ResponseTypeUtil;
import com.zzl.util.StringEmptyUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;

public class StudentSaveServlet extends HttpServlet {
    DbUtil dbUtil=new DbUtil();
    StudentServices studentService=new StudentServicrImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String stuNo=req.getParameter("stuNo");
        System.out.println(stuNo);
        String stuName=req.getParameter("stuName");
        System.out.println(stuName);
        String sex=req.getParameter("sex");
        System.out.println(sex);
        String birthday=req.getParameter("birthday");
        System.out.println(birthday);
        String gradeId=req.getParameter("gradeId");
        System.out.println(gradeId);
        String email=req.getParameter("email");
        System.out.println(email);
        String stuDesc=req.getParameter("stuDesc");
        System.out.println(stuDesc);
        String stuId=req.getParameter("stuId");
        System.out.println(stuId);

        Student student=new Student();
        try {
            student.setStuNo(stuNo);
            student.setStuName(stuName);
            student.setSex(sex);
            student.setBirthday(DateUtil.formatString(birthday,"yyyy-MM-dd"));
            student.setGradeId(Integer.parseInt(gradeId));
            student.setEmail(email);
            student.setStuDesc(stuDesc);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(StringEmptyUtil.isNotEmpty(stuId)){
            student.setStuID(Integer.parseInt(stuId));
        }
        Connection con=null;
        try {
            con=dbUtil.getCon();
            int saveNum=0;
            JSONObject result=new JSONObject();
            if(StringEmptyUtil.isNotEmpty(stuId)){
                saveNum=studentService.studentModify(con,student);
            }else {
                saveNum=studentService.studentAdd(con,student);
            }
            if(saveNum>0){
                result.put("success","true");
            }else {
                result.put("success","true");
                result.put("errorMsg","保存失败");
            }
            ResponseTypeUtil.write(resp,result);
        }
        catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                dbUtil.closeCon(con);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

